package com.yunshipei.adapter;


import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yunshipei.enterplorer.R;
import com.yunshipei.model.SSOSettingModel;

import java.util.List;

public class SSOSettingAdapter extends BaseAdapter {

    private Context context;
    private List<SSOSettingModel> ssoSettingModels;

    public SSOSettingAdapter(Context context, List<SSOSettingModel> ssoSettingModels) {
        this.context = context;
        this.ssoSettingModels = ssoSettingModels;
    }

    @Override
    public int getCount() {
        return ssoSettingModels == null ? 0 : ssoSettingModels.size();
    }

    @Override
    public SSOSettingModel getItem(int position) {
        return ssoSettingModels.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.list_view_sso_item, null);
            viewHolder = new ViewHolder();
            viewHolder.tvConfigure = (TextView) convertView.findViewById(R.id.tv_available);
            viewHolder.tvName = (TextView) convertView.findViewById(R.id.tv_sso_sys_name);
            viewHolder.ivNameImg = (ImageView) convertView.findViewById(R.id.iv_name_img);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        SSOSettingModel ssoSettingModel = getItem(position);
        String url = ssoSettingModel.getImgUrl();
        Glide.with(context).load(url).placeholder(R.drawable.collect_default_icon).into(viewHolder.ivNameImg);
        viewHolder.tvName.setText(ssoSettingModel.getName());
        Drawable right = context.getResources().getDrawable(R.drawable.right_head);
        right.setBounds(0, 0, right.getMinimumWidth(), right.getMinimumHeight());
        if (ssoSettingModel.isAvailable()) {
            Drawable drawable = context.getResources().getDrawable(R.drawable.icon_sso_protected);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            if (ssoSettingModel.isUnity()) {
                viewHolder.tvConfigure.setText("统一认证");
                viewHolder.tvConfigure.setCompoundDrawables(drawable, null, null, null);
                convertView.setEnabled(false);
                convertView.setBackgroundColor(Color.WHITE);
            } else {
                viewHolder.tvConfigure.setText("已配置");
                viewHolder.tvConfigure.setCompoundDrawables(drawable, null, right, null);
                convertView.setEnabled(true);
                convertView.setBackgroundResource(R.drawable.item_state_selector);
            }
        } else {
            viewHolder.tvConfigure.setText("未配置");
            Drawable drawable = context.getResources().getDrawable(R.drawable.icon_sso_no_protected);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            viewHolder.tvConfigure.setCompoundDrawables(drawable, null, right, null);
            convertView.setEnabled(true);
            convertView.setBackgroundResource(R.drawable.item_state_selector);
        }
        return convertView;
    }

    class ViewHolder {
        TextView tvName;
        TextView tvConfigure;
        ImageView ivNameImg;
    }
}
